// 计算最大值
export function calMax(arr) {
  let max = Math.max(...arr);
  let maxint = Math.ceil(max / 9.5);
  let maxval = maxint * 10;
  if (maxval == 0 || max < 1) {
    maxval = 1;
  }
  return maxval;
}

// 计算最小值
export function calMin(arr) {
  let min = Math.min(...arr);
  let minint = Math.floor(min / 10);
  let minval = minint * 10;
  return minval;
}

// 系列相关
export function getSeri({ type, name, color, ...others }) {
  let config = {
    type,
    name,
    itemStyle: { color },
  };
  if (type === 'line') {
    config = {
      ...config,
      symbol: 'none',
      lineStyle: { width: 1 },
    };
  } else if (type === 'bar') {
    config = {
      barMaxWidth: 20,
      ...config,
    };
  }
  return {
    ...config,
    ...others,
  };
}

// 基础配置
export const getOptions = ({ titleText, remSize, XAXIS, yAxis, grid, legendData, yGrid }) => {
  return {
    title: {
      text: titleText,
      show: true,
      top: 8 * remSize,
      left: -4 * remSize,
      textStyle: {
        fontSize: 14 * remSize,
        fontWeight: 300,
        lineHeight: 14 * remSize,
        color: '#FFFFFF',
      },
    },
    grid: grid || {
      top: 32 * remSize,
      left: ((yGrid || 36) + 10) * remSize,
      bottom: 46 * remSize,
      right: ((yGrid || 36) + 10) * remSize,
    },
    tooltip: {
      trigger: 'axis',
      padding: [12 * remSize, 17 * remSize],
      formatter: (params) => {
        let res = '<div  style="overflow-y:scroll;max-height:120px;font-size:12px">';
        params.forEach((item) => {
          res += `<span style=\"display:flex;align-items: center;justify-content: space-between;\">
            <span style=\"\">${item.marker}${item.seriesName}</span><span style=\"padding-left:20px\">`;
          res +=
            `${parseFloat(item.data).toFixed(3) === 'NaN' ? '-' : parseFloat(item.data)}` + '<br/>';
          res += `</span></span>`;
        });
        return `<span style=\"font-size:12px\">` + params[0].name + '</span><br/>' + res + '</div>';
      },
      position: function (point, params, dom, rect, size) {
        let x = 0;
        let y = 0;
        const pointX = point[0];
        const pointY = point[1];
        const boxWidth = size.contentSize[0];
        const boxHeight = size.contentSize[1];
        if (boxWidth > pointX) {
          x = pointX + 15;
        } else {
          x = pointX - boxWidth - 15;
        }
        if (boxHeight > pointY) {
          y = 5;
        } else {
          y = pointY - boxHeight;
        }
        return [x, y];
      },
      show: true,
      backgroundColor: '#161A1D',
      borderColor: '#161A1D',
      borderWidth: 1,
      textStyle: {
        color: '#A7A8A9',
        fontFamily: 'PingFang SC',
        fontStyle: 'none',
      },
    },
    legend: {
      right: 'center',
      top: 'bottom',
      width: '100%',
      itemGap: 13,
      itemWidth: 16,
      itemHeight: 8,
      textStyle: {
        color: 'white',
        fontSize: 12 * remSize,
        height: 14 * remSize,
        lineHeight: 17 * remSize,
      },
      padding: 0,
      data: legendData,
      inactiveColor: '#545454',
      inactiveBorderColor: '#545454',
      inactiveBorderWidth: 'inherit',
    },
    xAxis: {
      type: 'category',
      data: XAXIS.map((v) => v || ''),
      splitNumber: Math.random(),
      triggerEvent: true,
      boundaryGap: ['0%', '0%'],
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: 'rgba(24, 160, 251, 0.5)',
        },
      },
      axisLine: {
        show: true,
        onZero: false,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.4)',
        },
      },
      axisTick: {
        alignWithLabel: true,
        lineStyle: {
          color: '#4E5154',
        },
      },
      axisLabel: {
        color: 'rgba(255,255,255,0.6)',
        fontStyle: 'DIN-Light',
        formatter: (value) => {
          const _label = value.split('');
          return _label.length > 10 ? value.substring(0, 9) + '...' : value;
        },
        fontSize: 12 * remSize,
        lineHeight: 12 * remSize,
        margin: 9 * remSize,
      },
    },
    yAxis: yAxis || {
      type: 'value',
      axisLine: {
        show: false,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 12 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  };
};
// 场站总览
export function DeviationTariff(data = {}, remSize) {
  const {
    synthesizeQuantity = [], // 综合电量
    synthesizeElectrovalence = [], // 综合电价
    XAXIS = [],
  } = data;
  const yGrid =
    String(
      calMax([
        calMax(synthesizeQuantity), // 综合电量
        calMax(synthesizeElectrovalence), // 综合电价
      ]),
    ).length * 7;
  const yAxis = [
    {
      type: 'value',
      name: 'MWh',
      nameLocation: 'end',
      position: 'left',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
    {
      type: 'value',
      name: '元/MWh',
      nameLocation: 'end',
      position: 'right',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  ];
  const defaultLegendData = [
    {
      name: '综合电量',
      key: 'synthesizeQuantity',
      icon: 'roundRect',
    },
    {
      name: '综合电价',
      key: 'synthesizeElectrovalence',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#E6E963', color: '#272829' },
    },
  ];

  const legendData = [];
  defaultLegendData?.forEach((d) => {
    if (data[d?.key]) {
      legendData.push(d);
    }
  });
  const option = getOptions({
    remSize,
    XAXIS,
    yAxis,
    legendData,
    yGrid,
  });
  return {
    ...option,
    series: [
      getSeri({
        name: '综合电量',
        type: 'bar',
        data: synthesizeQuantity,
        color: '#18A0FB',
        barCategoryGap: 0,
      }),
      getSeri({
        name: '综合电价',
        type: 'line',
        symbol: 'circle',
        smooth: true,
        data: synthesizeElectrovalence,
        yAxisIndex: 1,
        color: '#E6E963',
        stack: 'A',
        lineStyle: { width: 1 },
      }),
    ],
  };
}
// 电量分析
export function DeductionIncome(data = {}, remSize) {
  const {
    mediumAndLongQuantity = [], // 中长期电量
    spotGoodsQuantity = [], // 计量电量
    XAXIS = [],
  } = data;
  const yGrid =
    String(
      calMax([
        calMax(spotGoodsQuantity), // 计量电量
        calMax(mediumAndLongQuantity), // 中长期电量
      ]),
    ).length * 7;
  const yAxis = [
    {
      type: 'value',
      name: 'MWh',
      nameLocation: 'end',
      position: 'left',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  ];
  const defaultLegendData = [
    {
      name: '计量电量',
      key: 'spotGoodsQuantity',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#18A0FB', color: '#272829' },
    },
    {
      name: '中长期电量',
      key: 'mediumAndLongQuantity',
    },
  ];
  const legendData = [];
  defaultLegendData?.forEach((d) => {
    if (data[d?.key]) {
      legendData.push(d);
    }
  });
  const option = getOptions({
    remSize,
    XAXIS,
    yAxis,
    legendData,
    yGrid,
  });
  return {
    ...option,
    series: [
      getSeri({
        type: 'line',
        name: '计量电量',
        smooth: true,
        symbol: 'circle',
        color: '#18A0FB',
        data: spotGoodsQuantity,
      }),
      getSeri({
        type: 'bar',
        name: '中长期电量',
        smooth: true,
        symbol: 'circle',
        color: '#BFEBA2',
        data: mediumAndLongQuantity,
      }),
    ],
  };
}
// 电价分析
export function DeductionPrice(data = {}, remSize) {
  const {
    synthesizeElectrovalence = [], // 综合电价
    mediumAndLongElectrovalence = [], // 中长期电价
    wholeNetworkElectrovalence = [], // 全网统一出清电价
    spotGoodsElectrovalence = [], // 省内实时节点电价
    XAXIS = [],
  } = data;
  const yGrid =
    String(
      calMax([
        calMax(synthesizeElectrovalence), // 综合电价
        calMax(mediumAndLongElectrovalence), // 中长期电价
        calMax(wholeNetworkElectrovalence), // 全网统一出清电价
        calMax(spotGoodsElectrovalence), // 省内实时节点电价
      ]),
    ).length * 7;
  const yAxis = [
    {
      type: 'value',
      name: '元/MWh',
      nameLocation: 'end',
      position: 'left',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  ];
  const defaultLegendData = [
    {
      name: '综合电价',
      key: 'synthesizeElectrovalence',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#F6C826', color: '#272829' },
    },
    {
      name: '中长期电价',
      key: 'mediumAndLongElectrovalence',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#30D485', color: '#272829' },
    },
    {
      name: '用户侧结算参考点电价',
      key: 'wholeNetworkElectrovalence',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#F67E26', color: '#272829' },
    },
    {
      name: '实时节点电价',
      key: 'spotGoodsElectrovalence',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#18A0FB', color: '#272829' },
    },
  ];
  const legendData = [];
  defaultLegendData?.forEach((d) => {
    if (data[d?.key]) {
      legendData.push(d);
    }
  });
  const option = getOptions({
    remSize,
    XAXIS,
    yAxis,
    legendData,
    yGrid,
  });
  return {
    ...option,
    series: [
      getSeri({
        name: '综合电价',
        type: 'line',
        symbol: 'circle',
        smooth: true,
        data: synthesizeElectrovalence,
        color: '#F6C826',
        lineStyle: { width: 1 },
      }),
      getSeri({
        name: '中长期电价',
        type: 'line',
        symbol: 'circle',
        smooth: true,
        data: mediumAndLongElectrovalence,
        color: '#30D485',
        lineStyle: { width: 1 },
      }),
      getSeri({
        name: '用户侧结算参考点电价',
        type: 'line',
        symbol: 'circle',
        smooth: true,
        data: wholeNetworkElectrovalence,
        color: '#F67E26',
        lineStyle: { width: 1 },
      }),
      getSeri({
        name: '实时节点电价',
        type: 'line',
        symbol: 'circle',
        smooth: true,
        data: spotGoodsElectrovalence,
        color: '#18A0FB',
        lineStyle: { width: 1 },
      }),
    ],
  };
}
